package com.cryptape.cita_wallet.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.alm.token.R;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.util.RootUtil;
import com.cryptape.cita_wallet.util.db.SharePrefUtil;

import java.util.Locale;

;

/**
 * Created by duanyytop on 2018/6/12
 */
public class SplashActivity extends BaseActivity {

    public static final String EXTRA_FIRST = "extra_first";
    public static final String LOCK_TO_MAIN = "lock_to_main";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {
            finish();
            return;
        }
        setContentView(R.layout.activity_splash_mango);
        inLoginPage = true;
//        getVersion();
        if (RootUtil.isDeviceRooted()) {
            new AlertDialog.Builder(mActivity)
                    .setTitle(R.string.safe_hint)
                    .setMessage(R.string.root_hint)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    }).setNegativeButton(R.string.go_on, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            gotoMainPage();
                        }
                    }).setCancelable(false)
                    .create().show();
        } else {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    try {
                        sleep(1000);
                        gotoMainPage();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
        initLanguage();
    }

//    private void getVersion(){
//        TokenService.getVersion().subscribe(new Subscriber<VersionUpdateBean>() {
//            @Override
//            public void onCompleted() {
//
//            }
//            @Override
//            public void onError(Throwable e) {
//
//            }
//
//
//            @Override
//            public void onNext(VersionUpdateBean versionUpdate) {
//                int versionCode = MainActivity.getVersionCode(SplashActivity.this);
//                if (Integer.parseInt(versionUpdate.getData().getVersioncode()) <= versionCode) {
//                    return;
//                }
//                String apkUrl = versionUpdate.getData().getDownload();
//                UpdateAppBean updateAppBean = new UpdateAppBean();
//                updateAppBean.setApkFileUrl(apkUrl);//设置 apk 的下载地址
//                String path = "";
//                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) || !Environment.isExternalStorageRemovable()) {
//                    try {
//                        path = getExternalCacheDir().getAbsolutePath();
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                    if (TextUtils.isEmpty(path)) {
//                        path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
//                    }
//                } else {
//                    path = getCacheDir().getAbsolutePath();
//                }
//                updateAppBean.setTargetPath(path); //设置apk 的保存路径
//                updateAppBean.setHttpManager(new UpdateAppHttpUtil()); //实现网络接口，只实现下载就可以
//                UpdateAppManager.download(SplashActivity.this, updateAppBean, new DownloadService.DownloadCallback() {
//                    @Override
//                    public void onStart() {
//                        HProgressDialogUtils.showHorizontalProgressDialog(SplashActivity.this, "下载进度", false);
//                        Log.d("下载", "onStart() called");
//                    }
//
//                    @Override
//                    public void onProgress(float progress, long totalSize) {
//                        HProgressDialogUtils.setProgress(Math.round(progress * 100));
//                        Log.d("下载", "onProgress() called with: progress = [" + progress + "], totalSize = [" + totalSize + "]");
//                    }
//
//                    @Override
//                    public void setMax(long totalSize) {
//                        Log.d("下载", "setMax() called with: totalSize = [" + totalSize + "]");
//                    }
//
//                    @Override
//                    public boolean onFinish(File file) {
//                        HProgressDialogUtils.cancel();
//                        Log.d("下载", "onFinish() called with: file = [" + file.getAbsolutePath() + "]");
//                        return true;
//                    }
//
//                    @Override
//                    public void onError(String msg) {
//                        HProgressDialogUtils.cancel();
//                        Log.e("下载", "onError() called with: msg = [" + msg + "]");
//                    }
//
//                    @Override
//                    public boolean onInstallAppAndAppOnForeground(File file) {
//                        Log.d("下载", "onInstallAppAndAppOnForeground() called with: file = [" + file + "]");
//                        return false;
//                    }
//                });
//
//
//
//            }});
//    }

    private void gotoMainPage() {
        if (!TextUtils.isEmpty(SharePrefUtil.getCurrentWalletName())) {
            if (SharePrefUtil.getBoolean(ConstantUtil.FINGERPRINT, false)) {
                Intent intent = new Intent(mActivity, FingerPrintActivity.class);
                intent.putExtra(LOCK_TO_MAIN, true);
                startActivity(intent);
            } else {
                startActivity(new Intent(mActivity, MainActivity.class));
            }
        } else {
            Intent intent = new Intent(mActivity, AddWalletActivity.class);
            startActivity(intent);
        }
        finish();
    }

    private void initLanguage() {
//        LanguageKeeper.getInstance().attachBaseContext(this);
//        LanguageKeeper.attachBaseContext(this);
//        LanguageKeeper keeper = new LanguageKeeper(this);
//        LanguageKeeper.attachBaseContext(this,keeper.getLanguage());
//        switch (keeper.getLanguage()) {
//            case LanguageKeeper.LANGUAGE_CN:
//                changeAppLanguage(Locale.CHINA);
//                break;
//            case LanguageKeeper.LANGUAGE_EN:
//                changeAppLanguage(Locale.ENGLISH);
//                break;
//            case LanguageKeeper.LANGUAGE_JA:
//                changeAppLanguage(Locale.JAPAN);
//                break;
//            case LanguageKeeper.LANGUAGE_KO:
//                changeAppLanguage(Locale.KOREA);
//                break;
//            default:
//                Locale locale;
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                    locale = LocaleList.getDefault().get(0);
//                } else {
//                    locale = Locale.getDefault();
//                }
//                String str = locale.getLanguage();
//                if (!TextUtils.isEmpty(str) && str.toLowerCase().contains("en")) {
//                    changeAppLanguage(Locale.ENGLISH);
//                }else {
//                    changeAppLanguage(Locale.CHINA);
//                }
//                break;
//        }
    }

//    @Override
//    protected void attachBaseContext(Context base) {
//        Context myContext = LanguageKeeper.getInstance().attachBaseContext(base);
//        super.attachBaseContext(myContext);
//    }

    public void changeAppLanguage(Locale locale) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            Configuration configuration = getResources().getConfiguration();
            configuration.setLocale(locale);
        }else {
            DisplayMetrics metrics = getResources().getDisplayMetrics();
            Configuration configuration = getResources().getConfiguration();
            configuration.locale = locale;
            getResources().updateConfiguration(configuration, metrics);
        }
    }

}
